package com.demo.service.impl;

import com.demo.entity.Oil;
import com.demo.entity.Speed;
import com.demo.mapper.OilMapper;
import com.demo.service.OilService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OilServiceImpl implements OilService {


    @Autowired
    private OilMapper oilmapper;


    @Transactional
    //查询返回所有油田编号（oil_field），每个油田编号只返回一条。
    @Override
    public ArrayList<Map<String, Object>> getOilFieldListWithLocation() {
        return oilmapper.selectDistinctOilFieldWithLocation();
    }

//    @Transactional
    @Override
    public Oil getLatestInfo(String oil_field, String eui){
        Oil test = oilmapper.getLatestDataByFieldAndNode(oil_field, eui);
        return test;
    }

    @Transactional
    @Override
    public List<Oil> getLatestDataByOilField(String oil_field){
        return oilmapper.getLatestDataByOilField(oil_field);
    }

    @Transactional
    @Override
    public List<Oil> getDataByOilFieldAndNode(String oil_field, String eui){
        return oilmapper.getDataByOilFieldAndNode(oil_field, eui);
    }

//    @Transactional
//    @Override
//    public String insertWiFiStatusByFieldAndNode(String oil_field, String eui, String time, boolean wifi_status){
////        Oil oil=getLatestInfo(oil_field, eui);
////        System.out.println(oil);
////        if (oil != null) {
////            oil.setTime(time);
////            oil.setWifistatus(wifi_status);
////           // oilmapper.insertOil(oil_field, eui, time, oil.getVoltage(), oil.getMemory(), oil.getLongitude(), oil.getLatitude(), oil.isWifistatus(), oil.isNodefault());
////            oilmapper.insertOil(oil);
////            return "Success";
////        } else {
////            return "Failed";
////        }
//       oilmapper. insertOil(oil_field, eui, time , wifi_status) ;
//       return "Success";
//    }

    @Transactional
    @Override
    public void updateWiFiStatusByFieldAndNode(String oil_field, String eui,  boolean wifi_status){
        oilmapper.updateWiFiStatusByFieldAndNode(oil_field, eui, wifi_status);
    }

    @Transactional
    @Override
    public int getNodeFaultByFieldAndNode(String oil_field, String eui){
        return oilmapper.getNodeFaultByFieldAndNode(oil_field, eui);
    }

    @Override
    public List<Oil> getLatestDataByFieldAndNodeAndFault(String oil_field, String eui, Integer node_status) {
        return oilmapper.getLatestDataByFieldAndNodeAndFault(oil_field, eui, node_status);
    }



    @Transactional
    @Override
    public int countOilFields(){
        return oilmapper.countOilFields();
    }

    @Transactional
    public  List<Map<String, Object>> getNodeCountByOilField(){
        return  oilmapper.getNodeCountByOilField();
    }

    @Transactional
    public int counteuis(){
        return  oilmapper.counteuis();
    }

    @Transactional
    public List<Map<String, Object>> getNodeCountsByFault(){
        return oilmapper.getNodeCountsByFault();
    }

    @Transactional
    public Integer getTotalNormalNodeCount(){
        return oilmapper.getTotalNormalNodeCount();
    }

    @Transactional
    public Integer getTotalFaultNodeCount(){
        return oilmapper.getTotalFaultNodeCount();
    }

    public  List<Oil> getFaultInfo( String oil_field,  boolean node_fault){
        return oilmapper.getFaultInfo(oil_field, node_fault);
    }
}